using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Net;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.OData;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
[Authorize]
public class UsersController : ODataController
{
    private SampleContext _db;
    private IUserService _userService;
    public UsersController(SampleContext context, IUserService service)
    {
        _db = context;
        _userService = service;
    }
    [AllowAnonymous]
    [HttpGet("/api/login")]
    public IActionResult Login(string number, string password)
    {
        var a = Request;
        var user = _userService.Authenticate(number, password);
        if (user == null)
            return BadRequest( "Username or password is incorrect" );
        else
            return Ok(user);
    }
    [EnableQuery]
    public IActionResult Get()
    {
        return Ok(_db.Users);
    }
    //

    //
}
[Authorize]
public class RoomsController : ODataController
{
    private SampleContext _db;
    public RoomsController(SampleContext context)
    {
        _db = context;

    }
    private bool RoomExists(int key)
    {
        return _db.Rooms.Any(p => p.Id == key);
    }
    [HttpPut]
    public async Task<IActionResult> Put([FromODataUri] int key, [FromBody]Room update)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }
        if (key != update.Id)
        {
            return BadRequest();
        }
        _db.Entry(update).State = EntityState.Modified;
        try
        {
            await _db.SaveChangesAsync();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!RoomExists(key))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }
        return Updated(update);
    }
    public async Task<IActionResult> Delete([FromODataUri] int key)
    {
        var product = await _db.Rooms.FindAsync(key);
        if (product == null)
        {
            return NotFound();
        }
        _db.Rooms.Remove(product);
        await _db.SaveChangesAsync();
        return NoContent();
    }
    [HttpPost]
    public async Task<IActionResult> Post([FromBody] Room model)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }
        _db.Rooms.Add(model);
        await _db.SaveChangesAsync();

        return Created(model);
    }
    [EnableQuery]
    public IActionResult Get()
    {
        return Ok(_db.Rooms);
    }
    //
}